﻿using System;
using System.Web;
using System.Collections;
using System.Text;
using System.Data;
using DDSC.OracleADO;

/// <summary>
/// DDSCGrid 的摘要描述
/// </summary>
public class DDSCGrid
{
    //Grid公用屬性
    public class PageInfo
    {
        public string OrderBy { get; set; }
        public int? PageIndex { get; set; }
        public int? PageSize { get; set; }
        public string TargetDiv { get; set; }
    }

    public static object ADOGridHandler(StringBuilder p_sb, ArrayList p_arrParam, PageInfo p_PageInfo)
    {
        int pageSize = p_PageInfo.PageSize.HasValue ? p_PageInfo.PageSize.Value : 10; //每頁幾筆 
        int pageNum = p_PageInfo.PageIndex.HasValue ? p_PageInfo.PageIndex.Value : 1;  //第幾頁
        int m_Position = (Convert.ToInt32(pageNum) - 1) * Convert.ToInt32(pageSize);
        if (p_PageInfo.OrderBy != "")
            p_sb.Append(" Order By " + p_PageInfo.OrderBy);

        dbAccess m_dba = new dbAccess();
        StringBuilder m_sbCount = new StringBuilder();
        Object results = new Object();

        DataSet ds = m_dba.runExecuteQuery("AUT_PROGRAM", p_sb.ToString(), m_Position, pageSize, p_arrParam);
        if ((ds.Tables["info"].Rows[0]["ReturnCode"].ToString() != "100" && ds.Tables["info"].Rows[0]["ReturnCode"].ToString() != "999"))
        {
            ArrayList m_arrParam = p_arrParam;
            System.Data.DataSet ds_Count = new System.Data.DataSet();
            m_sbCount.Append("select count(*) RowCount from ( " + p_sb.ToString() + " ) ");
            ds_Count = m_dba.runExecuteQuery("AUT_PROGRAM", m_sbCount.ToString(), m_arrParam);
            var totalRecords = Convert.ToInt32(ds_Count.Tables["AUT_PROGRAM"].Rows[0]["RowCount"].ToString());
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize); //總共幾頁

            results = m_dba.RowsToDictionary(ds.Tables["AUT_PROGRAM"], totalRecords.ToString(), totalPages.ToString());
        }
        else
            results = m_dba.RowsToDictionary(ds.Tables["info"], "0", "0");

        return (results);

    }

	public DDSCGrid()
	{
		//
		// TODO: 在這裡新增建構函式邏輯
		//
	}
}